package com.ternnetwork.workflow.controller;

import java.security.Principal;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ternnetwork.commons.model.http.BaseResponse;
import com.ternnetwork.core.accesscontroll.entity.User;
import com.ternnetwork.core.accesscontroll.service.UserService;
import com.ternnetwork.workflow.dto.ProcessTaskQueryDto;
import com.ternnetwork.workflow.service.ProcessTaskService;


/**
 * 流程任务控制器
 * @author xuwenfeng
 *
 */
@Controller
@RequestMapping("/process/task/*")
public class ProcessTaskController {
	
	
	@Autowired
	private ProcessTaskService processTaskService;
	
	@Autowired
	private UserService userService;
	
	/**
	 * 分页查询所有任务
	 * @param pageable
	 * @param dto
	 * @return
	 */
	@RequestMapping("/findByPage")
	public @ResponseBody BaseResponse findByPage(@PageableDefault Pageable pageable,ProcessTaskQueryDto dto) {
		return processTaskService.findByPage(pageable, dto);
	}
	
	/**
	 * 查询当前用户待办任务
	 * @param pageable
	 * @param dto
	 * @param user
	 * @return
	 */
	@RequestMapping("/findForAssigneeByPage")
	public @ResponseBody BaseResponse findForAssigneeByPage(@PageableDefault Pageable pageable,ProcessTaskQueryDto dto,Principal user) {
		dto.setTaskAssignee(user.getName());
		return processTaskService.findByPage(pageable, dto);
	}
	
	/**
	 * 查询当前用户候选任务
	 * @param pageable
	 * @param dto
	 * @param user
	 * @return
	 */
	@RequestMapping("/findForCandidateUserByPage")
	public @ResponseBody BaseResponse findForCandidateUserByPage(@PageableDefault Pageable pageable,ProcessTaskQueryDto dto,Principal user) {
		dto.setTaskCandidateUser(user.getName());
		return processTaskService.findByPage(pageable, dto);
	}
	
	/**
	 * 查询当前用户组候选任务
	 * 目前以用户角色代码为组标识，
	 * 多个角色代码用逗号分隔
	 * @param pageable
	 * @param dto
	 * @param user
	 * @return
	 */
	@RequestMapping("/findForCandidateGroupByPage")
	public @ResponseBody BaseResponse findForCandidateGroupByPage(@PageableDefault Pageable pageable,ProcessTaskQueryDto dto,Principal user) {
    	User dbUser=userService.findByName(user.getName());
		String code=dbUser.getRoleCodes().replaceAll(" ", ",");
		dto.setTaskCandidateGroup(code);
		return processTaskService.findByPage(pageable, dto);
	}
	
	
	
	/**
	 * 获取任务表单数据
	 * @param taskId
	 * @return
	 */
	@RequestMapping("/getTaskFormData")
	public @ResponseBody BaseResponse getTaskFormData(String taskId) {
		return processTaskService.getTaskFormData(taskId);
	}
	
	/**
	 * 领取任务
	 * @param id
	 * @param user
	 * @return
	 */
	@RequestMapping("/claim")
	public @ResponseBody BaseResponse claim(String id,Principal user) {
		return processTaskService.claim(id, user.getName());
	}
	
	/**
	 * 完成任务,保存表单数据
	 * @param id 任务ID
	 * @param request 请求
	 * @return
	 */
	@RequestMapping("/complete")
	public @ResponseBody BaseResponse complete(String id,HttpServletRequest request) {
		Map<String,String[]> map=request.getParameterMap();
		return processTaskService.complete(id, map);
	}
	
	/**
	 * 查找任务历史
	 * @param pageable 分页
	 * @param processInstanceId 流程实例ID
	 * @return
	 */
	@RequestMapping("/findHistoricTaskInstanceByPage")
	public @ResponseBody BaseResponse getHistoricTaskInstanceList(@PageableDefault Pageable pageable,String processInstanceId) {
		return processTaskService.findHistoricTaskInstanceByPage(pageable, processInstanceId);
	}
	
	
	/**
	  * 设置代理（转处理）
	  * @param id
	  * @param user 当前受理人
	  * @param userId 接受代理的用户
	  * @return
	  */
	 @RequestMapping("/delegate")
	 public @ResponseBody BaseResponse delegateTask(String id,Principal user,String userId) {
		 return processTaskService.delegateTask(id, user.getName(), userId);
	 }
	 
	 
	 /**
	  * 取销任务领取
	  * @param id
	  * @param user
	  * @return
	  */
	 @RequestMapping("/unclaim")
	 public @ResponseBody BaseResponse unclaim(String id,Principal user) {
		 return processTaskService.unclaim(id, user.getName());
	 }

}
